#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N=1e7+5;
int n,ans,mod;
inline int Mod(int x){return x>=mod?x-mod:x;}
inline int poww(int x,int y){
	int sum=1;
	while(y){
		if(y&1)sum=1ll*sum*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return sum;
}
int P[N],fac[N],inv[N],f[N];
void Init(){
	for(int i=P[0]=1;i<=n;i++)P[i]=Mod(P[i-1]+P[i-1]);
	for(int i=fac[0]=1;i<=n;i++)fac[i]=1ll*i*fac[i-1]%mod;
	inv[n]=poww(fac[n],mod-2);for(int i=n;i;i--)inv[i-1]=1ll*i*inv[i]%mod;
	f[n]=poww(n,mod-2);for(int i=n-1;i>=2;i--)f[i]=1ll*n*f[i+1]%mod;
	for(int i=2;i<=n;i++)f[i]=1ll*f[i]*i%mod;
}
signed main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	cin>>n>>mod;
	Init();
	for(int i=2;i<=n;i++)ans=Mod(ans+1ll*fac[n-2]*inv[n-i]%mod*f[i]%mod*P[n-i]%mod);
	ans=1ll*ans*n%mod*(n-1)%mod;
	ans=Mod(ans+1ll*poww(n,n-2)*P[n-1]%mod*n%mod);
	cout<<ans;
	return 0;
}
